package com.teambsc4.todo.model

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.teambsc4.todo.entities.TaskItem

/**
 * @projectName:  ROOM database
 * @package: com.teambsc4.todo.model
 * @className: MyDatabase
 * @description: TODO
 * @version: 1.0
 */
@Database(entities = [TaskItem::class], version = 1)
abstract class MyDatabase : RoomDatabase() {

    abstract fun taskItemDao(): TaskItemDao

    companion object {
        private var database: MyDatabase? = null

        @Synchronized
        fun getDatabase(context: Context): MyDatabase {
            database?.let {
                return it
            }
            return Room.databaseBuilder(
                context.applicationContext,
                MyDatabase::class.java,
                "MyTask"
            )
                .allowMainThreadQueries()
                .build().apply {
                    database = this
                }
        }
    }
}

